1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import java.util.Map;
22  
23  import javax.annotation.Nullable;
24  
25  /**
26   * Implementation of {@link ImmutableMultiset} with zero or more elements.
27   *
28   * @author Jared Levy
29   * @author Louis Wasserman
30   */
31  @GwtCompatible(serializable = true)
32  @SuppressWarnings("serial")
33  // uses writeReplace(), not default serialization
34  class RegularImmutableMultiset<E> extends ImmutableMultiset<E> {
35    private final transient ImmutableMap<E, Integer> map;
36    private final transient int size;
37  
38    RegularImmutableMultiset(ImmutableMap<E, Integer> map, int size) {
39      this.map = map;
40      this.size = size;
41    }
42  
43    @Override
44    boolean isPartialView() {
45      return map.isPartialView();
46    }
47  
48    @Override
49    public int count(@Nullable Object element) {
50      Integer value = map.get(element);
51      return (value == null) ? 0 : value;
52    }
53  
54    @Override
55    public int size() {
56      return size;
57    }
58  
59    @Override
60    public boolean contains(@Nullable Object element) {
61      return map.containsKey(element);
62    }
63  
64    @Override
65    public ImmutableSet<E> elementSet() {
66      return map.keySet();
67    }
68  
69    @Override
70    Entry<E> getEntry(int index) {
71      Map.Entry<E, Integer> mapEntry = map.entrySet().asList().get(index);
72      return Multisets.immutableEntry(mapEntry.getKey(), mapEntry.getValue());
73    }
74  
75    @Override
76    public int hashCode() {
77      return map.hashCode();
78    }
79  }